# coding=utf-8
import time
from boto3 import Session

from core.m_logging import logger

aws_key = "AKIAIOFGB7H4GDSE5NJQ"
aws_secret = "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"
bucket_name = "core-products"
session = Session(aws_access_key_id=aws_key, aws_secret_access_key=aws_secret)
s3 = session.resource('s3')
bucket = s3.Bucket(bucket_name)


class S3StampOutput(object):
    def __init__(self, s3_key, flush_size=1000):
        self.s3_key = s3_key
        self.flush_size = flush_size
        self.length = 0

    def output(self, content):
        self.length += 1
        logger.info("stamp length is %s..." % self.length)
        if self.length >= self.flush_size:
            self._write_to_s3()
            self.length = 0

    def _write_to_s3(self):
        timestamp = time.time()
        logger.info("stamp to s3://%s/%s..." % (bucket_name, self.s3_key))
        bucket.Object(key=self.s3_key).put(Body=str(timestamp))
        print('a')
